#include "logic_state.h"
#include "logic_state_scene_select.h"
#include "logic_manager.h"
#include "scene_manager.h"
#include "pugixml\pugixml.hpp"
#include "common.h"
#include "physics_manager.h"
#include "gui_button.h"
#include "gui_manager.h"
#include "pass_manager.h"
#include "gui_enum.h"
#include <iostream>
using namespace std;

logic_state_scene_select::logic_state_scene_select()
: is_scene_config_read(false)
, current_scene_index(0)
, scene_count(0)
{

}

logic_state_scene_select::~logic_state_scene_select()
{

}

void logic_state_scene_select::update(float dt)
{
	gui_manager::get_instance()->get_gui()->Update(dt);

	for(int i=0; i<scene_count; i++)
	{
		if (gui_manager::get_instance()->get_button(SELECT_BUTTON+i)->GetState() == GUI_BUTTON_UP)
		{
			gui_manager::get_instance()->get_button(SELECT_BUTTON+i)->SetState(GUI_NORMAL);
            goto_scene(i);
		}
	}

	if (gui_manager::get_instance()->get_button(BACK_TO_MAIN_BUTTON)->GetState() == GUI_BUTTON_UP)
	{
		logic_manager::get_instance()->change_state_to(STATE_MAIN_MENU);
	}
}

void logic_state_scene_select::enter()
{
    if (!is_scene_config_read)
	{
		read_scene_config();
		gui_manager::get_instance()->get_button(SELECT_BUTTON+0)->SetLock(false);
	}
	scene_manager::get_instance()->set_screen_fade_inout(SCREEN_FADE_IN);
	gui_manager::get_instance()->only_active_group(SELECT_BUTTON_GUI_GROUP);
}

void logic_state_scene_select::goto_scene(int idx)
{
	physics_manager::get_instance()->release_all_force();
	pass_manager::get_instance()->set_current_scene(idx);
    scene_manager::get_instance()->load_scene((char*)(scene_config[idx].c_str()));
	logic_manager::get_instance()->change_state_to(STATE_GAME_DOING);
}

void logic_state_scene_select::exit()
{

}

void logic_state_scene_select::read_scene_config()
{
	pugi::xml_document doc;
	doc.load_file("media/scene_config.xml");

	int button_pos_x = 150;
	int button_pos_y = 100;
	int button_pos_interval = 50;

	//traverse scenes
	pugi::xml_node scene_list_node = doc.child("scenes");
	for (pugi::xml_node scene_node = scene_list_node.child("scene"); scene_node; scene_node = scene_node.next_sibling())
	{
        string file((char*)(scene_node.attribute("file").value()));
		scene_config.push_back(file);
		//add select button (1~99)
		char f[13];
		sprintf(f, "media/%d.png", scene_count+1);
        gui_manager::get_instance()->add_button(SELECT_BUTTON_GUI_GROUP, SELECT_BUTTON+scene_count, f, button_pos_x, button_pos_y, 40, 40);
        gui_manager::get_instance()->get_button(SELECT_BUTTON+scene_count)->SetLock(true);
		
		scene_count++;
        button_pos_x += button_pos_interval;
		if (button_pos_x >= 650)
		{
			button_pos_x = 150;
            button_pos_y += button_pos_interval;
		}
	}

	pass_manager::get_instance()->set_scene_count(scene_count);
	gui_manager::get_instance()->add_button(SELECT_BUTTON_GUI_GROUP, BACK_TO_MAIN_BUTTON, "media/fanhuizhucaidan.png", 275, 500, 250, 50);
	is_scene_config_read = true;
}